package com.helloniu.mvc {

import com.helloniu.fl.NCHandler;
import com.helloniu.fl.SOHandler;
import com.helloniu.fl.util.Tool;

// template controller
public class TController {
	public function TController() {
	}

    public function setViewer(v:TViewer):void {
        v.setController(this);
        oneViewer = v;
        oneViewer.setEnabled(false);
    }

    public function initNC(nc:NCHandler, channelName:String):void {
    	ncHandler = nc;
        so = new SOHandler(nc.getNC(), channelName);
        so.setConnectSuccessListener(soConnected);
        so.setStatusListener(statusChange);
        so.connect();

        // @temp tonyy, 
        if(false) {
	        soOut = new SOHandler(nc, channelName + "OUT");
	        soOut.setStatusListener(statusChange);
	        soOut.connect();
        }
    }
	
	// called by viewer
    public function notifyModelInfoChanged(sth:*):void {
        // sync, server do sth
        so.setProperty("setData", sth);
    }

    private function soConnected(asoHandler:SOHandler):void {
    	oneViewer.setEnabled(true);
    }

    private function statusChange(asoHandler:SOHandler, pname:String, oldValue:*, 
    	newValue:*):void 
    {
        if(pname == null) {
            return;
        }

        newValue = asoHandler.getProperty(pname);

        if(pname == "setData") {
            updateModelInfo(newValue);
        }
    }

	// notify viewer to change as new model
    private function updateModelInfo(sth:*):void {    	
		if(oneViewer != null) {
			oneViewer.updateModelInfo(sth);
		}
		else {
			Tool.traceT("--error found---tviewer is null--");
		}
	}
	
	public function getConfID():String {
    	return ncHandler.getConfID();
    }
    
    public function getUserID():String {
    	return ncHandler.getUserID();
    }
    
    public function getNC():NCHandler {
    	return ncHandler;
    }

    private var oneViewer:TViewer;
    private var so:SOHandler = null;
    private var soOut:SOHandler = null;
    private var ncHandler:NCHandler = null;
}
}